﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>外部培训</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="~/lib/layui/css/layui.css" media="all">
    <script src="~/lib/layui/layui.js" charset="utf-8"></script>
</head>
<body>
        <form lay-filter="wbpx" class="layui-form layui-form-pane" action="">
            <input type="hidden" name="id">
            <input type="hidden" name="jbxxid">

            <div class="layui-form-item">
            <!--培训时间-->
                <div class="layui-inline">
                <label class="layui-form-label" style="width:160px">培训时间:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="pxdate" class="layui-input" placeholder="yyyy-MM-dd">
                    </div>
                </div>
            </div>
            <hr class="layui-border-green">
            <!--培训主题-->
            <div class="layui-form-item">
                <div class="layui-inline">
                <label class="layui-form-label" style="width:160px">培训主题</label>
                    <div class="layui-input-inline">
                        <input type="text" name="pxname" class="layui-input">
                    </div>
                </div>
            </div>
            <hr class="layui-border-green">

            <!--培训机构-->
            <div class="layui-form-item">
                <div class="layui-inline">
                <label class="layui-form-label" style="width:160px">培训机构</label>
                    <div class="layui-input-inline">
                        <input type="text" name="pxgs" class="layui-input">
                    </div>
                </div>
            </div>
            <hr class="layui-border-green">

            <!--培训讲师-->
            <div class="layui-form-item">
                <div class="layui-inline">
                <label class="layui-form-label" style="width:160px">培训讲师</label>
                    <div class="layui-input-inline">
                        <input type="text" name="pxr" class="layui-input">
                    </div>
                </div>
            </div>
            <hr class="layui-border-green">

            <!--培训费用-->
            <div class="layui-form-item">
                <div class="layui-inline">
                <label class="layui-form-label" style="width:160px">培训费用</label>
                    <div class="layui-input-inline">
                        <input type="text" name="pxfy" class="layui-input">
                    </div>
                </div>
            </div>
            <hr class="layui-border-green">

            <!--是否签订协议-->
            <div class="layui-inline">
            <label class="layui-form-label" style="width:160px">是否签订培训协议：</label>
                <div class="layui-input-inline">
                <select name="isxy" lay-filter="sfxy" lay-verify="required" lay-search="">
                        <option value="">下拉选择</option>
                        <option value="是">是</option>
                        <option value="否">否</option>
                    </select>
                </div>
            </div>
            <hr class="layui-border-green">

        <!--原因说明-->
        <div class="layui-form-item" id="note" style="display:none">
            <div class="layui-inline">
                <label class="layui-form-label" style="width:160px">原因说明：</label>
                <div class="layui-input-inline">
                    <input type="text" name="note" class="layui-input">
                </div>
            </div>
        </div>

            <!--培训期限-->
        <div class="layui-form-item" id="xyqx" style="display:none">
                <div class="layui-inline">
                <label class="layui-form-label" style="width:160px">服务期限</label>
                    <div class="layui-input-inline">
                        <input type="text" name="pxqx" class="layui-input">
                    </div>
                </div>
            </div>

            <!--培训协议-->
            <div class="layui-form-item" id="dpxxy" style="display:none">
                <div class="layui-upload">
                <button type="button" class="layui-btn layui-btn-normal" id="UPpxxy">选择《培训协议》</button>
                <button type="button" class="layui-btn" id="bt_UPpxxy">开始上传</button>
                    <input name="pxxy" id="pxxy" hidden="hidden" value="" />
                    <div class="layui-upload-list" style="max-width: 1000px;">
                    <table class="layui-table" id="tb_wbpx" lay-filter="tb_wbpx">
                            <colgroup>
                                <col>
                                <col width="150">
                                <col width="260">
                                <col width="150">
                            </colgroup>
                            <thead>
                                <tr>
                                    <th>文件名</th>
                                    <th>大小</th>
                                    <th>上传进度</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody id="pxxyList"></tbody>
                        </table>
                        <script type="text/html" id="barwbpx">
                            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="wbpx_del">删除</a>
                        </script>
                    </div>
                      
                </div>
            </div>
            <hr class="layui-border-green">

            <div class="layui-form-item">
                  <button type="button" class="layui-btn" id="Save_wbpx">保存</button>
            </div>
            <hr class="layui-border-green">
        </form>
        <script>
        layui.use(['form', 'util', 'laydate', 'dropdown','upload','table'], function () {
            var form = layui.form;
            var layer = layui.layer;
            var util = layui.util;
            var upload = layui.upload;
            var dropdown = layui.dropdown;
            var laydate = layui.laydate;
            var table = layui.table;
            var xyfile = [{
                "pxxy": "无"
            }]

            //初始赋值
            var url = location.search; 
            if (url.indexOf("?") != -1) {  
                var id = getQueryVariable('id');
                var jbxxid = getQueryVariable('jbxxid');

                if(id!=null)
                {
                    var data = getwbpxData("", id);
                    if(data.length>0)
                    {
                        form.val('wbpx', {
                            "id": id,
                            "jbxxid": data[0].jbxxid,
                            "pxdate": data[0].pxdate,
                            "pxname": data[0].pxname,
                            "pxr": data[0].pxr,
                            "pxgs": data[0].pxgs,
                            "pxfy": data[0].pxfy,
                            "pxqx": data[0].pxqx,
                            "pxxy": data[0].pxxy,
                            "isxy": data[0].isxy,
                            "note": data[0].note
                        });

                        laydate.render({
                            elem: '#wbpxtime'
                            , value: data[0].pxdate
                        });

                        if (data[0].isxy == "是") {
                            $("#xyqx").show();
                            $("#dpxxy").show();
                            $("#note").hide();
                        }
                        else {
                            $("#xyqx").hide();
                            $("#dpxxy").hide();
                            $("#note").show();
                        }

                        if (data[0].isxy == "是") {
                            $("#xyqx").show();
                            $("#dpxxy").show();
                            //加载附件列表
                            if(data[0].pxxy != null)
                            {
                                var WorkFiles = data[0].pxxy.split(',');
                                var xyfile = [];
                                for (i = 0; i < WorkFiles.length; i++) {
                                //    var tr = $(['<tr id="upload-' + i + '">'
                                //        //, '<td>' + file.name + '</td>'
                                //        , '<td><input type="button" style="border: 0;color:blue" readonly class="xcname" type="text"  value="' + WorkFiles[i] + '" onclick = "window.open(\'/UploadFiles/' + WorkFiles[i] + '\')"></td>'
                                //        //, '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>'
                                //        //, '<td><div class="layui-progress" lay-filter="progress-demo-' + index + '"><div class="layui-progress-bar" lay-percent=""></div></div></td>'
                                //        , '<td>'
                                //        //, '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                                //        //, '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'          
                                //        , '</td>'
                                //        , '< th lay - data="{fixed: \'right\', width:178, align:\'center\', toolbar: \'#barDemo\'}" > </th>'
                                //        , '</tr>'].join(''));

                                //    $("#pxxyList").append(tr);

                                    xyfile.push({ "pxxy": WorkFiles[i] });
                                }

                                //加载数据
                                table.render({
                                    elem: '#tb_wbpx'
                                    , cols: [[ //标题栏
                                        { field: "pxxy", title: '文件名', width: 150, templet: addlink }
                                        , { fixed: 'right', title: '操作', width: 125, minWidth: 125, toolbar: '#barwbpx' }
                                    ]]
                                    , data: xyfile//form.val('ldht').id)
                                    , toolbar: ''
                                    , even: true
                                });
                            }



                        }
                    }
                } else {
                    form.val('wbpx', {
                        "jbxxid": jbxxid
                    });

                    laydate.render({
                        elem: '#wbpxtime'
                        , value: getNow()
                    });
                }


            }




            //事件监听
            table.on('tool(tb_wbpx)', function (obj) {
                var data = obj.data;
                switch(obj.event){
                    case 'showRec':
                        var data = obj.data;//获取监听点击当前行的所有信息[object，object]
                        var url = "/UploadFiles/" + data.pxxy;
                        window.open(url);
                    break;
                    case 'wbpx_del':
                        var pxxy = document.getElementById('pxxy').value;
                        pxxy = pxxy.replace(data.pxxy, "");
                        var beginstr = pxxy.charAt(0);
                        if (beginstr == ",")
                            pxxy = pxxy.slice(1);
                        var endstr = pxxy.substr(-1);
                        if (endstr == ",")
                            pxxy = pxxy.slice(0, pxxy.length - 1);
                        document.getElementById('pxxy').value = pxxy.replace(",,", ",");
                        obj.del();
                    break;
                }
            });

            form.on('select(sfxy)', function (data) {
                if (data.value == "是") {
                    $("#xyqx").show();
                    $("#dpxxy").show();
                    $("#note").hide();
                }
                else {
                    $("#xyqx").hide();
                    $("#dpxxy").hide();
                    $("#note").show();
                }
            });

            //上传培训计划
            var uploadListIns = upload.render({
                elem: '#UPpxxy'
                , elemList: $('#pxxyList') //列表元素对象
                , url: '/upload' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
                , accept: 'file'
                , multiple: true
                , number: 3
                , auto: false
                , bindAction: '#bt_UPpxxy'
                , choose: function (obj) {
                    var that = this;
                    var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列            

                    //读取本地文件
                    obj.preview(function (index, file, result) {

                        var tr = $(['<tr id="upload-' + index + '">'
                            //, '<td>' + file.name + '</td>'
                            , '<td><input style="border: 0;" readonly class="xcname" type="text"  value="' + file.name + '"></td>'
                            , '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>'
                            , '<td><div class="layui-progress" lay-filter="progress-demo-' + index + '"><div class="layui-progress-bar" lay-percent=""></div></div></td>'
                            , '<td>'
                            , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                            , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                            , '</td>'
                            , '</tr>'].join(''));

                        //单个重传
                        tr.find('.demo-reload').on('click', function () {
                            obj.upload(index, file);
                        });

                        //删除
                        tr.find('.demo-delete').on('click', function () {
                            var tags = [];
                            $(".xcname").each(function (i, e) {
                                tags[i] = $(this).val();
                            });
                            document.getElementById('pxxy').value += "," + tags.toString();
                            delete files[index]; //删除对应的文件
                            tr.remove();
                            uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                        });

                        that.elemList.append(tr);
                        element.render('progress'); //渲染新加的进度条组件
                    });

                }
                , done: function (res, index, upload) { //成功的回调
                    var that = this;
                    if (res.code == 0) { //上传成功
                        layer.msg("上传成功！");
                        var tr = that.elemList.find('tr#upload-' + index)
                            , tds = tr.children();
                        tds.eq(3).html(''); //清空操作

                        var tags = [];
                        $(".xcname").each(function (i, e) {
                            tags[i] = $(this).val();
                        });
                        document.getElementById('pxxy').value += ","+tags.toString();
                        document.getElementById('pxxy').value = document.getElementById('pxxy').value.replace(",,",",");
                        delete this.files[index]; //删除文件队列已经上传成功的文件
                        return;
                    }
                    this.error(index, upload);
                }
                , allDone: function (obj) { //多文件上传完毕后的状态回调
                    console.log(obj)
                }
                , error: function (index, upload) { //错误回调
                    var that = this;

                    var tags = [];
                    $(".xcname").each(function (i, e) {
                        tags[i] = $(this).val();
                    });
                    document.getElementById('pxxy').value = tags.toString();


                    var tr = that.elemList.find('tr#upload-' + index)
                        , tds = tr.children();
                    tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
                }
                , progress: function (n, elem, e, index) { //注意：index 参数为 layui 2.6.6 新增
                    element.progress('progress-demo-' + index, n + '%'); //执行进度条。n 即为返回的进度百分比
                }
            });

            //外部培训保存
            layui.$('#Save_wbpx').on('click', function () {
                var data = form.val('wbpx');

                if(data.jbxxid==""){
                    layer.msg("请先保存基本信息！");
                    return;
                }

                var opurl = "/HmcWH/Save_wbpx";

                if (form.val('wbpx').id != "") {
                    opurl = "/HmcWH/Update_wbpx";
                } else {
                    form.val('wbpx', {
                        "id": guiid()
                    });
                }
                if (form.val('wbpx').pxxy == "NaN") {
                    form.val('wbpx', {
                        "pxxy": ""
                    });
                }
                data = form.val('wbpx');
                                            
                $.ajax({
                    url: opurl,
                    type: "post",
                    async: false,
                    data: data,
                    success: function (result) {
                        var index = parent.layer.getFrameIndex(window.name);
                        layer.msg(result, { time: 1000 });
                        parent.layer.close(index);
                        if (result == "保存成功！" || result == "修改成功！") {
                            //刷新父级表格
                            parent.layui.table.reload('wbpx', {
                                data: getwbpxData(data.jbxxid, "")
                            });
                        }
                        if (result == "保存失败！") {
                            form.val('wbpx', {
                                "id": ""
                            });
                            return;
                        }

                    }
                });
            });
        });

        function getOrgTreeData() {
            var data = [];
            $.ajax({
                url: "/TreeData/GetOrgTreeDatanew",
                type: "post",
                async: false,
                success: function (resut) {
                    data = resut;
                }
            });
            return data;
        }

        function guiid() {
            return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,
                function (c) {
                    var r = (Math.random() * 16) | 0,
                        v = c == 'x' ? r : (r & 0x3) | 0x8;
                    return v.toString(16);
                }
            );
        }

        function getwbpxData(jbxxid, wbpxid) {
            var data = [];
            $.ajax({
                url: "/HmcWH/get_wbpx",
                type: "post",
                async: false,
                data: { "jbxxid": jbxxid, "wbpxid": wbpxid },
                success: function (result) {
                    data = result;
                }
            });
            return data;
        }

        function getNow() {
            var nowdate = new Date();
            var year = nowdate.getFullYear(),
                month = nowdate.getMonth() + 1,
                date = nowdate.getDate();

            return year + "-" + month + "-" + date;
        }

        function getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) { return pair[1]; }
            }
            return null;
        }

        var addlink = function (d) {
            var html = '<div><a rel="nofollow" style="color:#1E9FFF" href="javascript:void(0);" lay-event="showRec">' + d.pxxy + '</a></div>';
            return html
        }
        </script>

</body>
</html>